package com.ecoolex.union.pay.retailer.ws;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.union.pay.retailer.core.manager.UnionPayPopularMerchantProductManager;
import com.ecoolex.union.pay.retailer.core.util.unionpay.LogUtil;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchantProduct;
import com.ecoolex.union.pay.retailer.pojo.AddPopularMerchantProductDTO;
import com.ecoolex.union.pay.retailer.pojo.UnionPayPopularMerchantProductDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class UnionPayPopularMerchantProductServiceImpl implements UnionPayPopularMerchantProductService {

    @Autowired
    UnionPayPopularMerchantProductManager unionPayPopularMerchantProductManager;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
	Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder;

	private ObjectMapper objectMapper;

	@PostConstruct
	public void init() {
		objectMapper = jacksonObjectMapperBuilder
				.dateFormat(new SimpleDateFormat("yyyyMMddHHmmss", new Locale("zh", "cn"))).build();
	}

    @Override
    public List<UnionPayPopularMerchantProductDTO> getPopularProducts(String merchantId) {
        String key = UnionPayPopularMerchantService.PRODUCT +":"+ merchantId;
        Long size = redisTemplate.opsForValue().size(key);
        List<UnionPayPopularMerchantProductDTO> dtos = new ArrayList(size.intValue());
		try {
			if (size > 0) {
				String json = redisTemplate.opsForValue().get(key);
				List<UnionPayPopularMerchantProductDTO> temp = objectMapper.readValue(json, dtos.getClass());
				dtos.addAll(temp);
			}
			else {
				dtos = unionPayPopularMerchantProductManager.getPopularProducts(merchantId);
				String json = objectMapper.writeValueAsString(dtos);
				redisTemplate.opsForValue().set(key, json);
			}
		}
		catch (Exception e) {
			LogUtil.writeErrorLog("", e);
        }
        return dtos;
    }

    @Override
    public List<UnionPayPopularMerchantProduct> queryMerchantProduct(String merchantId) {
        return unionPayPopularMerchantProductManager.getPopularProduct(merchantId);
    }

    @Override
    public Pagination<UnionPayPopularMerchantProduct> queryMerchantProduct(String merchantId, Page page) {
        return unionPayPopularMerchantProductManager.queryMerchantProduct(merchantId,page);
    }

    @Override
    public UnionPayPopularMerchantProduct addMerchantProduct(AddPopularMerchantProductDTO record, String userName) {
        return unionPayPopularMerchantProductManager.addMerchantProduct(record,userName);
    }

    @Override
    public UnionPayPopularMerchantProduct get(Integer id) {
        return unionPayPopularMerchantProductManager.get(id);
    }

    @Override
    public UnionPayPopularMerchantProduct editMerchantProduct(UnionPayPopularMerchantProduct old, String userName) {
        return unionPayPopularMerchantProductManager.editMerchantProduct(old,userName);
    }

    @Override
    public void delete(UnionPayPopularMerchantProduct product, String userName) {
        unionPayPopularMerchantProductManager.delete(product, userName);
    }
}
